package com.hj.saapp.control.service;

import android.app.Activity;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


import com.hj.saapp.BCDApplication;
import com.hj.saapp.common.manager.AppManager;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Logger;

import static android.bluetooth.BluetoothDevice.ACTION_FOUND;

public class BluetoothService {
	private Context context = null;
	private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	public static  BluetoothService bluetoothService;
	BluetoothImp bluetoothImp;

	public static BluetoothService getBluetoothService(BluetoothImp bluetoothImp) {
		bluetoothService = new BluetoothService();
		bluetoothService.bluetoothImp = bluetoothImp;
		return bluetoothService;
	}


	



		public BluetoothService() {


			this.context = AppManager.getAppManager().getActivity();
		}

	
	
	private void initIntentFilter() {

		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(ACTION_FOUND);
		intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
		intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
		intentFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
		intentFilter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
		intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
		context.registerReceiver(receiver, intentFilter);
	}

	//打开蓝牙	
	public void openBluetooth(){
		Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
		AppManager.getAppManager().getActivity().startActivityForResult(enableIntent, 1);
	}
	
	//关闭蓝牙	
	public void closeBluetooth(){
		this.bluetoothAdapter.disable();
	}
	
	//判断蓝牙是否打开	
	public boolean isOpen(){
		return this.bluetoothAdapter.isEnabled();
	}
	
	//搜索蓝牙设备
	public void searchDevices(){


		this.bluetoothAdapter.startDiscovery();
	}


	
	//蓝牙广播接收器	
	private BroadcastReceiver receiver = new BroadcastReceiver() {
		

		
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			com.orhanobut.logger.Logger.e(action);


			if(ACTION_FOUND.equals(action)){
				BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				bluetoothImp.found(device);
			} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){

				bluetoothImp.scanStart();

			} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
				bluetoothImp.scanFinish();

			} else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {

				bluetoothImp.request();
			}else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
				//当设备的连接状态改变
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				switch (device.getBondState()) {
					case BluetoothDevice.BOND_BONDING:
						bluetoothImp.pairStart();
						com.orhanobut.logger.Logger.e("开始配对");
						break;
					case BluetoothDevice.BOND_BONDED:
						bluetoothImp.pairFinish();

						break;
					case BluetoothDevice.BOND_NONE:
						bluetoothImp.pairError();

					default:
						break;
				}
			}
			
			if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
				if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {

					bluetoothImp.on();

				} else if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {

					bluetoothImp.off();

				}
			}
		}
	};

	public void unregister()
	{
		context.unregisterReceiver(receiver);
	}
	public void register()
	{
		initIntentFilter();
	}



	public interface BluetoothImp
	{
		public void found(BluetoothDevice device);
		public void scanStart();
		public void pairStart();
		public void scanFinish();
		public void pairFinish();
		public void pairError();
		public void on();
		public void off();
		public void request();

	}

	
}
